﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 算法基本功.算法._0_数组._1_双指针.旋转数组
{
    internal class 旋转数组
    {

        //顺时针90°旋转矩阵
        public void Rotate(int[][] matrix)
        {
            int n = matrix.Length;
            //先进行对角线的翻转
            for (int i = 0; i < n; i++)
            {
                for (int j = i; j < n; j++)
                {
                    // swap(matrix[i][j], matrix[j][i]);
                    int temp = matrix[j][i];
                    matrix[j][i] = matrix[i][j];
                    matrix[i][j] = temp;

                    //如果是逆时针则是副对角线的翻转 swap(matrix[i][j], matrix[n-j-1][n-i-1])

                }
            }

            //再头尾行交换
            foreach (int[] row in matrix)
            {
                reverse(row);
            }

            //按行头尾交换一维数组
            void reverse(int[] nums)
            {
                int l = 0, r = nums.Length - 1;
                while (l < r)
                {
                    int temp = nums[l];
                    nums[l] = nums[r];
                    nums[r] = temp;
                    l++;
                    r--;
                }
            }
        }

    }

}
